package com.yami.shop.personnft.platform.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.mail.MailException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.PersonNftApply;
import com.yami.shop.bean.model.ProdNft;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.personnft.common.model.TransferBatchApply;
import com.yami.shop.personnft.common.service.PersonNftApplyService;
import com.yami.shop.personnft.common.service.TransferBatchApplyService;
import com.yami.shop.personnft.common.vo.NftPersonVO;
import com.yami.shop.service.ProdNftService;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * spu信息
 *
 * @author FrozenWatermelon
 * @date 2020-10-28 15:27:24
 */
@RestController("adminPersonnftApplyController")
@RequestMapping("/admin/personnftApply")
@Tag(name = "admin-personnftApply信息")
@Slf4j
public class PersonnftApplyController {
	@Autowired
    private PersonNftApplyService personNftApplyService;
	@Autowired
	private UserService userService;
	@Autowired
	private ProdNftService dawInfoService;
	@Autowired
	private ProductService productService;
	@Autowired
	private TransferBatchApplyService transferBatchApplyService;

	@GetMapping("/selectListPage")
	@Operation(summary =  "获取平台信息列表", description = "分页获取平台信息列表")
	public ServerResponseEntity<IPage<PersonNftApply>> selectListPage(PageParam pageDTO, PersonNftApply personNftApply) {
		IPage<PersonNftApply> page = personNftApplyService.page(pageDTO, personNftApply);
		List<PersonNftApply> list = page.getRecords();
		if(list!=null&&list.size()>0){
			for (PersonNftApply snap:list) {
				//获取名字
				User userInfo = userService.getUserByUserId(snap.getUserId());
				if(userInfo!=null){
					snap.setUserName(userInfo.getNickName());
				}
				//如果是转增获取接收人名称
				if(snap.getSceneType().equals("1")){
					User toUserInfo = userService.getUserByUserId(snap.getToUserId());
					if(toUserInfo!=null){
						snap.setToUserName(toUserInfo.getNickName());
					}
				}
				try {
					ProdNft dataByDawId = dawInfoService.getDataByProdId(snap.getProductId());
					if (ObjectUtil.isNotEmpty(dataByDawId)){
						Product product = productService.getProductByProdId( dataByDawId.getProdId());
						//获取商品名称
						if(ObjectUtil.isNotEmpty(product)){
							snap.setProductName(product.getProdName());
						}
					}
				}catch (Exception e){
					log.info(e.getMessage());
				}

				if(snap.getStatus()==7){
					snap.setNftPercentage(personNftApplyService.getLastSendData(snap.getId()));
				}
			}
		}
		page.setRecords(list);
		return ServerResponseEntity.success(page);
	}

	/**
	 * 划转申请
	 ** @param apply
	 * @return
	 * @description: 转仓
	 */
	@PostMapping("/changeCeceiptApply")
	@Operation(summary =  "划转申请审核意见", description = "划转申请审核意见")
	public ServerResponseEntity changeCeceiptApply(@RequestBody PersonNftApply apply) {
		PersonNftApply personNftApply = personNftApplyService.getinfoById(apply.getId());
		if(personNftApply==null){
			throw new YamiShopBindException("yami.sys.illegalArgument");
		}
		if(personNftApply.getStatus() != 0 || !personNftApply.getSceneType().equals("3")){
			throw new YamiShopBindException("yami.sys.illegalArgument");
		}
		Integer count = personNftApply.getCount();
		if(count<=0){
			throw new YamiShopBindException("yami.sys.illegalArgument");
		}
		if(!apply.getStatus().equals(2) && !apply.getStatus().equals(1)){
			throw new YamiShopBindException("yami.sys.illegalArgument");
		}
		try {
			personNftApplyService.changeCeceiptApply(apply.getId(), apply.getStatus().toString(), apply.getRemark());
		}catch (MailException e){
			return ServerResponseEntity.showFailMsg(e.getMessage());
		}catch (Exception e){
			return ServerResponseEntity.showFailMsg("划转异常");
		}
		return ServerResponseEntity.success();
	}

	/**
	 * 划转申请
	 ** @param personNftApply
	 * @return
	 * @description: 转仓批量
	 */
	@GetMapping( "/changeCeceiptApplyAll")
	public ServerResponseEntity changeCeceiptApplyAll(PersonNftApply personNftApply) {
		try {
			personNftApply.setChannelType("1");
			List<PersonNftApply> list = personNftApplyService.listByParam(personNftApply);
			if(list.size()==0){
				throw new YamiShopBindException("yami.personnft.thansfer.nftFromTo.check");
			}
			for (PersonNftApply nftApply:list) {
				personNftApplyService.changeCeceiptApply(nftApply.getId(),"1",null);
			}
			return ServerResponseEntity.success();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("异常: " + e.getMessage() + "\r\n" + Arrays.toString(e.getStackTrace()));
			throw new YamiShopBindException(e.getMessage());
		}
	}

	/**
	 * 划转发送
	 ** @param id
	 * @return
	 * @description: 转仓
	 */
	@RequestMapping( "/changeCeceiptApplySend")
	@ResponseBody
	public ServerResponseEntity changeCeceiptApplySend(@RequestParam("id")String id) {
		try {
			TransferBatchApply transferBatchApply = transferBatchApplyService.selectIntoStautsByBatchId(Long.valueOf(id));
			if(transferBatchApply==null){
				throw new YamiShopBindException("yami.sys.illegalArgument");
			}
			if(transferBatchApply.getStatus()==7){
				throw new YamiShopBindException("划转等待中");
			}
			if(transferBatchApply.getStatus()!=1){
				throw new YamiShopBindException("yami.sys.illegalArgument");
			}
			personNftApplyService.sendAndDeletePersonNfts(Long.valueOf(id));
			return ServerResponseEntity.success();
		} catch (Exception e) {
			e.printStackTrace();
			log.error("异常: " + e.getMessage() + "\r\n" + Arrays.toString(e.getStackTrace()));
			throw new YamiShopBindException(e.getMessage());
		}
	}


	@GetMapping("/ua/gets")
	@Operation(summary =  "获取会员持仓情况", description = "获取会员持仓情况")
	public ServerResponseEntity gets(){
		personNftApplyService.taskChekSendDataResult();
		return ServerResponseEntity.success();
	}
}
